# coding: utf-8
from datetime import datetime
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey
from app.extensions import db

"""
数据库信息配置表
"""
class DatabaseInfoConfig(db.Model):
    __tablename__ = 'sjml_database_config'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键ID')
    system_id = Column(Integer, ForeignKey('system_access_config.id'), nullable=False, comment='系统接入配置ID，外键')
    platform_type = Column(String(100), comment='平台类型')
    db_type = Column(String(100), nullable=False, comment='数据库类型')
    db_version = Column(String(100), nullable=False, comment='数据库版本')
    db_connect_str = Column(String(255), comment='数据库连接串')
    ip = Column(String(100), nullable=False, comment='IP地址')
    port = Column(String(20), nullable=False, comment='端口')
    db_name = Column(String(100), nullable=False, comment='数据库名称')
    db_username = Column(String(100), nullable=False, comment='数据库用户名')
    db_password = Column(String(100), nullable=False, comment='数据库密码')
    create_time = Column(DateTime, default=datetime.now, nullable=False, comment='创建时间')

    def to_dict(self):
        return {
            'id': self.id,
            'system_id': self.system_id,
            'platform_type': self.platform_type,
            'db_type': self.db_type,
            'db_version': self.db_version,
            'db_connect_str': self.db_connect_str,
            'ip': self.ip,
            'port': self.port,
            'db_name': self.db_name,
            'db_username': self.db_username,
            'db_password': self.db_password,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
        }
